package com.example.accessingdatajpa.service.impl;

import com.example.accessingdatajpa.pojo.FirstPage;
import com.example.accessingdatajpa.repository.FirstPageRepository;
import com.example.accessingdatajpa.response.RestResponse;
import com.example.accessingdatajpa.service.IFirstPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service("firstPageService")
public class FirstPageService implements IFirstPageService {
    @Autowired
    private FirstPageRepository firstPageRepository;

    @Override
    @Transactional
    public RestResponse<List<FirstPage>> queryAllFirstPicUrl(){
        RestResponse<List<FirstPage>> response = new RestResponse<>();
        try {
            List<FirstPage> data = firstPageRepository.findAll();
            response.setStatus(200);
            response.setMessage("查询成功");
            response.setData(data);
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }

    @Override
    @Transactional
    public RestResponse<String> addOnePicUrl(FirstPage firstPage){
        RestResponse<String> response = new RestResponse<>();
        try {
            firstPageRepository.save(firstPage);
            response.setStatus(200);
            response.setMessage("ok");
            response.setData("保存成功");
        }catch (RuntimeException e){
            response.setStatus(500);
            response.setMessage(e.getMessage());
        }
        return response;
    }
}
